<template>
	<view class="user-container">
		<view class="card">
			<view class="card_header">
				<view class="card_header-avatar">
					<image src="../../static/logo.png" mode="aspectFit" role="img" class="card_header-avatar-image">
					</image>
				</view>
				<view v-if="!isRegister" class="card_header-content">
					<text class="card_header-content-title" @click="goRegister()">注册</text>
					<text class="card_header-content-subtitle">会员等级</text>
				</view>
				<view v-else class="card_header-content">
					<text class="card_header-content-title">{{nickName}}</text>
					<text class="card_header-content-subtitle">会员等级</text>
				</view>
				<view v-if="!isRegister" class="card_header-extra">
					<text class="card_header-extra-text">会员积分</text>
				</view>
				<view v-else class="card_header-extra">
					<text class="card_header-extra-text">{{gainedPoint}}</text>
				</view>
			</view>
		</view>
		<view class="turntable">
			<image src="../../static/choujiang.png" @click="goLotteryDraw()" class="mypic"></image>
		</view>
	</view>
</template>

<script>
	import {
		memberGet
	} from '../../api/index.js'//引入会员详情查询接口
	const miniProgramAppid = 'wx95df8d135409ab43';
	const secret = 'ce525ecb58eebf4aa11170b7bd3a3839';
	export default {
		data() {
			return {
				openid: '',
				code: '',
				nickName: '',
				gainedPoint: '',
				isRegister: false
			}
		},
		methods: {
			//1. 获取openid
			// 获取微信登录的授权码code
			wxLogin(e) {
				uni.login({
					provider: "weixin",
					success: (res) => {
						console.log(res);
						this.code = res.code;
						// 发送request请求，获取openid和session_key
						uni.request({
							url: 'https://api.weixin.qq.com/sns/jscode2session',
							method: 'GET',
							data: {
								'appid': miniProgramAppid,
								'secret': secret,
								'js_code': this.code,
								'grant_type': 'authorization_code'
							},
							success: (res) => {
								console.log(res.data);
								this.openid = res.data.openid;
								this.get();
								uni.setStorageSync('openid', this.openid);
							}
						})
					}
				})
			},
			//2. 查询会员是否注册
			get() {
				uni.showLoading({
					title: '加载中...'
				});
				memberGet({
					"data":{
						"appId": miniProgramAppid,
						"openId": this.openid
					}
				})
				.then(res => {
						uni.hideLoading();
						this.isRegister = true;
						this.nickName = res.data.data.nickname;
						this.gainedPoint = res.data.data.gainedPoint;
						// uni.showToast({
						// 	title: '登陆成功',
						// 	icon: 'success',
						// 	duration: 2000
						// })
				})
				.catch(res => {
						uni.showToast({
							title: '请先注册',
							icon: 'none',
							duration: 2000
						})
				})
			},
			goLotteryDraw() {
				uni.navigateTo({
					url: '/pages/index/lotteryDraw'
				})
			},
			goRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			onShow() {
				this.wxLogin()
			}
		}
	}
</script>

<style lang="scss">
	.user-container {
		overflow: hidden;
		font-family: HYQihei 40S;
		.card {
			margin: 15px;
			padding: 0 10px;
			box-shadow: 0px 0px 3px 1px rgb(150 141 141 / 8%);
			border: 1px solid #EBEEF5;

			.card_header {
				display: flex;
				border-bottom: 1px #EBEEF5 solid;
				flex-direction: row;
				align-items: center;
				padding: 10px;
				overflow: hidden;

				.card_header-avatar {
					width: 40px;
					height: 40px;
					overflow: hidden;
					border-radius: 5px;
					margin-right: 10px;

					.card_header-avatar-image {
						flex: 1;
						width: 40px;
						height: 40px;
					}
				}

				.card_header-content {
					display: flex;
					flex-direction: column;
					justify-content: center;
					flex: 1;
					overflow: hidden;

					.card_header-content-title {
						padding: 10px 0px 10px 10px;
						font-size: 15px;
						color: #3a3a3a;
					}

					.card_header-content-subtitle {
						padding: 0px 0px 0px 10px;
						font-size: 12px;
						margin-top: 5px;
						color: #909399;
					}
				}

				.card_header-extra {
					font-size: 12px;
					color: #909399;
				}
			}
		}

		.turntable {
			padding: 0rpx 35rpx 35rpx 35rpx;

			.mypic {
				width: 680rpx;
				height: 450rpx;
				box-shadow: 4rpx 4rpx 4rpx 8rpx #f0f0f0;
			}
		}
	}
</style>